<!-- 正文开始 -->
<div class="layui-fluid" id="userSearch">
    <div class="layui-card">
        <div class="layui-card-body">

            <div class="layui-form toolbar" lay-filter="userSearch">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <select name="searchCode">
                            <option value="">搜索条件</option>
                            <option value="userCode">账号</option>
                            <option value="userName">用户名</option>
                            <option value="mobile">手机号</option>
                            <option value="phone">座机号</option>
                        </select>
                    </div>
                    <div class="layui-inline">
                        <input name="searchValue" class="layui-input" type="text" placeholder="输入关键字"/>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn btnSearch"><i class="layui-icon">&#xe615;</i>搜索</button>
                        <button class="layui-btn icon-btn btnAdd"><i class="layui-icon">&#xe654;</i>添加</button>
                        <button class="layui-btn icon-btn btnSave layui-btn-normal"><i class="layui-icon layui-icon-ok"></i>保存</button>
                    </div>
                </div>
            </div>

            <table class="layui-table" id="userTable" lay-filter="userTable"></table>

        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="tpl-userTable-operator">
    <!--<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">修改</a>-->
    <a class="layui-btn layui-btn-xs" lay-event="reset">重置密码</a>
</script>

<!-- 表格状态列 -->
<script type="text/html" id="tpl-userTable-status">
    <input type="checkbox" lay-filter="tpl-userTable-status" value="{{d.id}}" lay-skin="switch" lay-text="正常|锁定"
           {{d.status==1 ? 'checked' : ''}}/>
</script>

<!-- 表格性别列 -->
<script type="text/html" id="tpl-userTable-sex">
    <input type="radio" lay-filter="tpl-userTable-sex" data-id="{{d.id}}" name="userTable-sex-{{d.id}}" value="0" title="男" {{d.sex==0 ? 'checked' : ''}}/>
    <input type="radio" lay-filter="tpl-userTable-sex" data-id="{{d.id}}" name="userTable-sex-{{d.id}}" value="1" title="女" {{d.sex==1 ? 'checked' : ''}}/>
</script>

<script>
    layui.use(['layer', 'table', 'form', 'http', 'tools'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var table = layui.table;
        var form = layui.form;
        var http = layui.http;
        var tools = layui.tools;

        var viewId = "#userSearch";
        var view = $(viewId);
        var userTableId = 'userTable';

        form.render('select','userSearch');
        // 渲染表格
        var userTableIns = table.render({
            id: userTableId,
            elem: '#userTable',
            url: http.base_server + 'user/list',
            method: 'POST',
            where: {
                // access_token: http.getToken().access_token
            },
            page: false,
            cellMinWidth: 50,
            height: 'full-200',
            cols: [[
                {type: 'numbers'},
                {field: 'id', title: 'ID', hide: true},
                {field: 'userCode', sort: true, title: '账号', width:100},
                {field: 'userName', sort: true, title: '用户名',width:100, edit: 'text'},
                {field: 'sexText', sort: true, title: '性别', width:50, hide: true},
                {field: 'mobile', sort: true, title: '手机号',width:100, edit: 'text'},
                {field: 'phone', sort: true, title: '座机号', width:100, edit: 'text', hide: true},
                {field: 'sex', sort: true, templet: '#tpl-userTable-sex', title: '性别',width:90, align:"center"},
                {field: 'photoUrl', title: '头像', width:200, edit: 'text'},
                {field: 'defaultPage', title: '默认主页',width:200, edit: 'text'},
                {field: 'status', sort: true, templet: '#tpl-userTable-status', title: '状态',width:70, align:"center"},
                {align: 'center', toolbar: '#tpl-userTable-operator', title: '操作', minWidth: 70},
                {field: 'updateTime', sort: true, title: '更新时间'},
                {field: 'updateByWho', sort: true, title: '更新人'},
            ]],
            error: function(code,msg){
                http.fail(code,msg);
            },
            done: function(res, curr, count){
            }
        });

        // 添加按钮点击事件
        view.find(".btnAdd").click(function () {
            var param = tools.param = {};
            param.action = 1;
            param.tableIns = userTableIns;
            showEditModel(true);
        });

        // 工具条点击事件
        table.on('tool(userTable)', function (rowObj) {
            var data = rowObj.data;
            var layEvent = rowObj.event;

            if (layEvent === 'edit') { // 修改
                // var param = tools.param = {};
                // param.action = 2;
                // param.rowObj = rowObj;
                // param.tableIns = userTableIns;
                // showEditModel(false);
            } else if (layEvent === 'reset') { // 重置密码
                layer.confirm('确定重置此用户的密码吗？',
                    {btn:["确定","取消"], skin: 'layui-layer-admin'}, function (i) {
                    layer.close(i);
                    http.post('user/reset', {
                        id: data.id
                    }, function (res) {
                        if (res.ok) {
                            http.okMsg(res.msg);
                        } else {
                        }
                    });
                });
            }
        });

        // 显示表单弹窗
        function showEditModel(isAdd) {
            tools.open({
                title: isAdd ? '添加用户': '修改用户',
                url: http.viewPath + '/system/userForm.html?v=1',
                success: function (layero, index) {
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');  // 禁止出现滚动条
                }
            });
        };

        var fnSearch = function(){
            var param = {};
            var searchCode = view.find("select[name='searchCode']").val();
            var searchValue = view.find("input[name='searchValue']").val();
            if(searchCode && searchCode.length>0){
                param[searchCode] = searchValue;
            }
            userTableIns.reload({where: param});
        }

        // 搜索按钮点击事件
        view.find(".btnSearch").click(function () {
            fnSearch();
        });

        // 修改user状态
        form.on('switch(tpl-userTable-status)', function (obj) {
            var idx = obj.othis.parents("tr:eq(0)").attr("data-index");
            var data = {id: obj.elem.value, status: obj.elem.checked ? 1 : 0};
            userTableIns.saveDelta(idx,data);
        });
        form.on('radio(tpl-userTable-sex)', function (obj) {
            var idx = obj.othis.parents("tr:eq(0)").attr("data-index");
            var data = {id: obj.elem.getAttribute("data-id"), sex: obj.elem.value};
            userTableIns.saveDelta(idx,data);
        });

        view.find(".btnSave").click(function () {
            var data = userTableIns.getDelta();
            if(!data){return;}
            http.post('user/saveBatch',JSON.stringify(data),function(res){
                if(res.ok){
                    userTableIns.updateCache(res.data);
                    userTableIns.resetDelta();
                   http.okMsg(res.msg);

                }
            });
        });
        table.on('edit(userTable)',function (row) {
            var idx = row.tr.attr("data-index");
            var data = {};
            data.id = row.data.id;
            data[row.field] = row.value;
            userTableIns.saveDelta(idx,data);
        })
    });
</script>